class EvenEmitter {
    constructor() {
        this.event = {}
    }

    // 实现订阅
    on(type, callback) {
        if(!this.event) this.event = Object.create(null)
        if(!this.event[type]) {
            this.event[type] = [callback]
        }
        else {
            this.event[type].push(callback)
        }
    }
    // 删除订阅
    off(type, callback) {
        if(!this.event[type]) return
        this.event[type] = this.event[type].filter(item => {
            return item != callback
        })
    }
    // 只执行一次订阅事件
    once(type, callback) {
        function fn() {
            // console.log(this);
            callback()
            this.off(type, fn)
        }
        this.on(type, fn)
    }
    // 触发事件
    // 注意this
    emit(type, ...rest) {
        this.event[type] && this.event[type].forEach(fn => {
            // console.log(this);
            fn.apply(this,rest)
        })
    }
}

const event1 = new EvenEmitter();

const handle = (...rest) => {
  console.log(rest);
};

event1.on("click", handle);

event1.emit("click", 1, 2, 3, 4);

event1.off("click", handle);

event1.emit("click", 1, 2);

event1.once("dbClick", () => {
  console.log(123456);
});
event1.emit("dbClick");
event1.emit("dbClick");
